package com.edroid.autorunner.hacker;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;


public class Hacker {
	private static final String str1 =
			"POST /register HTTP/1.1\r\n" + 
			"Host: www.chunyuyisheng.com\r\n" + 
			"Connection: keep-alive\r\n" + 
			"Content-Length: 144\r\n" + 
			"Cache-Control: max-age=0\r\n" + 
			"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" + 
			"Origin: null\r\n" + 
			"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36\r\n" + 
			"Content-Type: application/x-www-form-urlencoded\r\n" + 
			"DNT: 1\r\n" + 
			"Accept-Encoding: gzip,deflate\r\n" + 
			"Accept-Language: zh-CN,zh;q=0.8\r\n" + 
			"Cookie: csrftoken=%s\r\n" + 
			"\r\n" + 
			"csrfmiddlewaretoken=%s&callback=&username=%s&verify_code=1323&password1=%s&password2=%s";
	
	public static void regChunyuyisheng(String number) throws Exception{
		URL url = new URL("http://www.chunyuyisheng.com/register");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		String f = conn.getHeaderField("Set-Cookie");
		conn.disconnect();
			
		String csrf = f.substring("csrftoken=".length(), "csrftoken=56f80f85530db7a87446632c8b2c08ff".length());
		String postString = String.format(str1, csrf,csrf,number,number,number);
		System.out.println(postString);

		Socket socket = new Socket("www.chunyuyisheng.com",80);
		OutputStream out = socket.getOutputStream();
		out.write(postString.getBytes());
		out.flush();
		socket.getInputStream().read();
		socket.close();
	}
	
	
	public static void main(String[] args) throws Exception {
		regChunyuyisheng("13237574222");
//		method2();	
		
		
		
		
		
	}
	private static void method2() throws Exception {

		Socket socket = new Socket("www.chunyuyisheng.com",80);
		PrintWriter pw = new PrintWriter(socket.getOutputStream());
		pw.println("GET /register HTTP/1.1");
		pw.println("Host: www.chunyuyisheng.com");
		pw.println();
		pw.println();
		pw.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String line = null;
		while((line=br.readLine())!=null) {
			if(line.contains("csrfmiddlewaretoken")) {
				System.out.println(line.trim());
			}
		}
		br.close();
		socket.close();
	}
	private static void method1() throws Exception {
		URL url = new URL("http://www.chunyuyisheng.com/register");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		System.out.println(conn.getHeaderField("Set-Cookie"));
		
//		BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//		String line = null;
//		while((line=br.readLine())!=null) {
//			if(line.contains("csrfmiddlewaretoken")) {
//				System.out.println(line.trim());
//			}
//		}
		conn.disconnect();
	}
	

}
